<template>
  <basic-container>
    <el-card>
      <div style="margin-bottom: 20px;">
        请选择订阅政策的类型：
      </div>
      <avue-form :option="search_option" v-model="search_form" @submit="submit"></avue-form>
    </el-card>
    <avue-crud
      :data="data"
      :option="option"
      :page.sync="page"
      :table-loading="loading"
      @current-change="currentChange"
      @on-load="onLoad"
      @size-change="sizeChange"
      ref="crud"
      v-model="form">
    </avue-crud>
  </basic-container>
</template>

<script>
import {baseUrl} from "@/config/env";
import {getDetail, update} from "@/api/enterprise/companybasicinf"
import {getSubList} from "@/api/enterprise/cmsContent";

export default {
  data() {
    return {
      search_option: {
        subLoad:false,
        size: "mini",
        submitText: "订阅",
        gutter: 1,
        column: [
          {
            prop: 'id',
            display: false,
          },
          {
            label: '区域级政策',
            prop: 'area',
            type: 'checkbox',
            span: 24,
            dataType: 'string',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=policyArea',
            props: {
              label: "dictValue",
              value: "dictKey"
            }
          },
          {
            label: '产业类政策',
            prop: 'prod',
            type: 'checkbox',
            span: 24,
            dataType: 'string',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=policyProduct',
            props: {
              label: "dictValue",
              value: "dictKey"
            }
          },
          {
            label: '行业类政策',
            prop: 'industry',
            type: 'checkbox',
            span: 24,
            dataType: 'string',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=policyIndustry',
            props: {
              label: "dictValue",
              value: "dictKey"
            }
          },
        ]
      },
      search_form: {
        area: '',
        prod: '',
        industry: ''
      },
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        title: "已订阅的政策",
        menu: false,
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        delBtn: false,
        addBtn: false,
        border: true,
        index: false,
        viewBtn: false,
        selection: false,
        dialogClickModal: false,
        refreshBtn: false,
        columnBtn: false,
        searchBtn: false,
        align: "center",
        column: [
          {
            label: "标题",
            width: "800",
            prop: "contentTitle",
          },
          {
            label: "类型",
            prop: "contentArea",
            dataType: "string",
            dicUrl: baseUrl + "/blade-system/dict/dictionary?code=policyArea",
            props: {
              label: "dictValue",
              value: "dictKey"
            }
          },
          {
            label: "发布时间",
            prop: "createTime"
          }
        ]
      },
      data: []
    };
  },
  methods: {
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad() {
      getDetail().then(res => {
        console.log('HH', res)
        this.search_form = Object.assign(this.search_form, res.data.data);
        this.subLoad=true;
        this.onLoadSubList(this.search_form);
      }, error => {
        window.console.log(error)
      })
    },
    onLoadSubList() {
      if (this.subLoad) {
        this.loading = true;
        const {area: contentArea, prod: contentProduct, industry: contentIndustry} = this.search_form;
        getSubList(this.page.currentPage, this.page.pageSize, {
          contentArea,
          contentProduct,
          contentIndustry
        }).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        })
        this.loading = false;
      }
    },
    submit(form, done) {
      update(form).then(() => {
        // 重新查询文章
        this.onLoadSubList(this.search_form);
        this.$message.success('订阅成功');
      }, () => {
        this.$message.success('订阅失败,请联系管理员');
      })
      done();
    }
  }
};
</script>

<style>
</style>
